const db = require('../models/db')
const moment = require('moment')

//修改某个用户对于某个评论的点赞情况
exports.update =async (req,res,next)=>{
  try{
    const {id} = req.params;
    // console.log(id);
    const {user_id,isLike} = req.body;
    const sqlStr =
        `  UPDATE likes
           SET isLike = ${isLike}
           WHERE user_id = ${user_id} AND comment_id=${id}
        `;
    await db.query(sqlStr);
    const like = await db.query(`SELECT * FROM likes WHERE user_id = ${user_id} AND comment_id=${id}`);
    res.status(201).json(like)
  }catch (err) {
    next(err)
  }
};

//每一次有用户追加了一条新的父评论,就需要去初始化所有用户对当前父评论的点赞情况
exports.create = async (article_id,comment_id,next)=>{
  try {
    const sqlStr =
    `
       INSERT INTO likes(user_id,article_id,comment_id,isLike)
       SELECT u.id AS user_id,
       ${article_id},
       ${comment_id},
       0
       FROM users AS u
    `;
    await db.query(sqlStr);

  } catch (err) {
    next(err)
  }
}
